package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.dto.Information;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvposMapper;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.utils.ConvertPageInfoUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    @Override
    public Information getList(YddAdvpos yddAdvpos, String page, String limit) {
        int courrentPage = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);
        IPage<YddAdvpos> advposIPage = new Page(courrentPage, pageSize);
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(yddAdvpos.getAdvposdesc()), YddAdvpos::getAdvposdesc, yddAdvpos.getAdvposdesc());
        wrapper.like(StringUtils.isNotEmpty(yddAdvpos.getAdvposname()), YddAdvpos::getAdvposname, yddAdvpos.getAdvposname());
        page(advposIPage, wrapper);
        List<YddAdvpos> yddAdvposes = advposIPage.getRecords();
        List<YddAdvposDto> yddAdvposDtos = new ArrayList<>();
        for (YddAdvpos yddAdvpose : yddAdvposes) {
            LambdaQueryWrapper<YddAdvimg> wrapper1 = new LambdaQueryWrapper<>();
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(yddAdvpose, yddAdvposDto);
            Integer id = yddAdvpose.getId();
            wrapper1.eq(YddAdvimg::getAdvimgpos, id);
            int count = yddAdvimgService.count(wrapper1);
            yddAdvposDto.setAdvposnum(count);
            yddAdvposDtos.add(yddAdvposDto);
        }
        Information information = ConvertPageInfoUtil.getPage(advposIPage, yddAdvposDtos);
        return information;
    }

}
